package com.ky.manage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ky.manage.R;
import com.ky.manage.model.response.MountingsResp;

import java.util.HashMap;
import java.util.List;

public class OrderMountingsAdapter extends BaseAdapter {

    List<MountingsResp.RowsDTO> mMountings;
    Context mContext;
    MoountingsCountListner listner;
    HashMap<Integer, Integer> countMaps = new HashMap<>();

    public OrderMountingsAdapter(Context context, List<MountingsResp.RowsDTO> mountings, MoountingsCountListner listner) {
        mContext = context;
        mMountings = mountings;
        this.listner = listner;
    }

    @Override
    public int getCount() {
        return mMountings.size();
    }

    @Override
    public Object getItem(int position) {
        return mMountings.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.item_mountings_for_netpoint, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.iv_mountings_img);
            viewHolder.tvProductName = convertView.findViewById(R.id.tv_mountings_name);
            viewHolder.tvProductPrice = convertView.findViewById(R.id.tv_mountings_price);
            viewHolder.tvAddition = convertView.findViewById(R.id.tv_addition);
            viewHolder.tvMinus = convertView.findViewById(R.id.tv_minus);
            viewHolder.tvCount = convertView.findViewById(R.id.tv_count);
            viewHolder.count = 0;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MountingsResp.RowsDTO mountings = mMountings.get(position);

        Glide.with(mContext).load(mountings.getPicUrl()).error(R.mipmap.ic_launcher).into(viewHolder.imageView);
        viewHolder.tvProductName.setText(mountings.getMountingsName());
        viewHolder.tvProductPrice.setText("¥" + mountings.getUnitPrice());
        int count = countMaps.get(position) == null ? 0 : countMaps.get(position);
        if (count <= 0) {
            viewHolder.tvCount.setText(String.valueOf(0));
        } else {
            viewHolder.tvCount.setText(String.valueOf(count));
        }

        viewHolder.tvAddition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = countMaps.get(position) == null ? 0 : countMaps.get(position);
                countMaps.put(position, count + 1);
                viewHolder.tvCount.setText(String.valueOf(count + 1));
                listner.OnAddition(count + 1, mMountings.get(position));
            }
        });

        viewHolder.tvMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = countMaps.get(position) == null ? 0 : countMaps.get(position);
                if (count <= 0) {
                    viewHolder.tvCount.setText("0");
                    countMaps.put(position, 0);
                    listner.OnMinus(0, mMountings.get(position));
                } else {
                    viewHolder.tvCount.setText(String.valueOf(count - 1));
                    countMaps.put(position, count - 1);
                    listner.OnMinus(count - 1, mMountings.get(position));
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        int count = 0;
        ImageView imageView;
        TextView tvProductName;
        TextView tvProductPrice;
        TextView tvAddition;
        TextView tvMinus;
        TextView tvCount;
    }

    public void setData(List<MountingsResp.RowsDTO> mountings) {
        this.mMountings = mountings;
        notifyDataSetChanged();
    }

    public interface MoountingsCountListner {
        void OnAddition(int count, MountingsResp.RowsDTO obj);

        void OnMinus(int count, MountingsResp.RowsDTO obj);
    }
}
